package com.ruoyi.find.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.find.domain.Furniture;
import com.ruoyi.find.service.IFurnitureService;
import com.ruoyi.find.service.IFurnitureTypeService;


/**
 * 家具信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@Tag(name = "家具信息管理", description = "家具信息相关接口")
@RestController
@RequestMapping("/find/furniture")
public class FurnitureController extends BaseController
{
    @Autowired
    private IFurnitureService furnitureService;

    @Autowired
    private IFurnitureTypeService furnitureTypeService;

    /**
     * 查询家具信息列表
     */
    @Operation(summary = "查询家具信息列表", description = "获取所有家具信息的分页列表")
    @PreAuthorize("@ss.hasPermi('find:furniture:list')")
    @GetMapping("/list")
    public TableDataInfo list(Furniture furniture)
    {
        startPage();
        List<Furniture> list = furnitureService.selectFurnitureList(furniture);
        return getDataTable(list);
    }

    /**
     * 导出家具信息列表
     */
    @Operation(summary = "导出家具信息列表", description = "将家具信息列表导出为Excel文件")
    @PreAuthorize("@ss.hasPermi('find:furniture:export')")
    @Log(title = "家具信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Furniture furniture)
    {
        List<Furniture> list = furnitureService.selectFurnitureList(furniture);
        ExcelUtil<Furniture> util = new ExcelUtil<Furniture>(Furniture.class);
        util.exportExcel(response, list, "家具信息数据");
    }

    /**
     * 获取家具信息详细信息
     */
    @Operation(summary = "获取家具信息详细信息", description = "根据家具ID获取家具详细信息")
    @GetMapping(value = "/{furnitureId}")
    public AjaxResult getInfo(@PathVariable("furnitureId") Long furnitureId)
    {
        return AjaxResult.success(furnitureService.selectFurnitureByFurnitureId(furnitureId));
    }

    /**
     * 新增家具信息
     */
    @Operation(summary = "新增家具信息", description = "创建新的家具信息记录")
    @PreAuthorize("@ss.hasPermi('find:furniture:add')")
    @Log(title = "家具信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Furniture furniture)
    {
        // 设置创建人
        furniture.setCreateBy(SecurityUtils.getUsername());
        // 获取家具类型名称
        if (furniture.getFurnitureTypeId() != null) {
            String typeName = furnitureTypeService.selectFurnitureTypeByTypeId(furniture.getFurnitureTypeId()).getTypeName();
            furniture.setFurnitureTypeName(typeName);
        }
        return toAjax(furnitureService.insertFurniture(furniture));
    }

    /**
     * 修改家具信息
     */
    @Operation(summary = "修改家具信息", description = "更新指定的家具信息")
    @PreAuthorize("@ss.hasPermi('find:furniture:edit')")
    @Log(title = "家具信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Furniture furniture)
    {
        // 获取家具类型名称
        if (furniture.getFurnitureTypeId() != null) {
            String typeName = furnitureTypeService.selectFurnitureTypeByTypeId(furniture.getFurnitureTypeId()).getTypeName();
            furniture.setFurnitureTypeName(typeName);
        }
        return toAjax(furnitureService.updateFurniture(furniture));
    }

    /**
     * 删除家具信息
     */
    @PreAuthorize("@ss.hasPermi('find:furniture:remove')")
    @Log(title = "家具信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{furnitureIds}")
    public AjaxResult remove(@PathVariable Long[] furnitureIds)
    {
        return toAjax(furnitureService.deleteFurnitureByFurnitureIds(furnitureIds));
    }

    /**
     * 根据交易类型查询家具
     */
    @GetMapping("/byTransactionType/{transactionType}")
    public AjaxResult selectFurnitureByTransactionType(@PathVariable Integer transactionType)
    {
        List<Furniture> list = furnitureService.selectFurnitureByTransactionType(transactionType);
        return AjaxResult.success(list);
    }

    /**
     * 查询我的发布
     */
    @PreAuthorize("@ss.hasPermi('find:furniture:myPublish')")
    @GetMapping("/myPublish")
    public TableDataInfo selectMyPublish()
    {
        startPage();
        List<Furniture> list = furnitureService.selectFurnitureByCreateBy(SecurityUtils.getUsername());
        return getDataTable(list);
    }

    /**
     * 搜索家具
     */
    @GetMapping("/search")
    public TableDataInfo searchFurniture(String keyword)
    {
        startPage();
        List<Furniture> list = furnitureService.searchFurniture(keyword);
        return getDataTable(list);
    }

    /**
     * 上架家具
     */
    @PreAuthorize("@ss.hasPermi('find:furniture:edit')")
    @Log(title = "家具信息", businessType = BusinessType.UPDATE)
    @PutMapping("/up/{furnitureId}")
    public AjaxResult upFurniture(@PathVariable Long furnitureId)
    {
        return toAjax(furnitureService.upFurniture(furnitureId));
    }

    /**
     * 下架家具
     */
    @PreAuthorize("@ss.hasPermi('find:furniture:edit')")
    @Log(title = "家具信息", businessType = BusinessType.UPDATE)
    @PutMapping("/down/{furnitureId}")
    public AjaxResult downFurniture(@PathVariable Long furnitureId)
    {
        return toAjax(furnitureService.downFurniture(furnitureId));
    }

    /**
     * 获取出售中的家具列表
     */
    @GetMapping("/forSale")
    public TableDataInfo getForSaleFurniture()
    {
        startPage();
        Furniture furniture = new Furniture();
        furniture.setTransactionType(1); // 出售
        furniture.setStatus(1); // 上架
        List<Furniture> list = furnitureService.selectFurnitureList(furniture);
        return getDataTable(list);
    }

    /**
     * 获取出租中的家具列表
     */
    @GetMapping("/forRent")
    public TableDataInfo getForRentFurniture()
    {
        startPage();
        Furniture furniture = new Furniture();
        furniture.setTransactionType(2); // 租赁
        furniture.setStatus(1); // 上架
        List<Furniture> list = furnitureService.selectFurnitureList(furniture);
        return getDataTable(list);
    }
}